package com.lyt.springboot.util.algorithm;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.*;
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;

public class S34 {
    static class MyThread extends Thread {
        @Override
        public void run() {
            System.out.println("hello mythread");
        }
    }

    static class MyThread1 implements Runnable {
        @Override
        public void run() {
            System.out.println("hell my1");
        }
    }

    public static void main(String[] args) {
        new MyThread().start();

        new Thread(new MyThread1()).start();

        new Thread(() -> {
            System.out.println("hello my2");
        }).start();
    }


    static void testSleep() {
        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("A" + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    static void testYield() {
        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                if (i % 100 == 0) {
                    Thread.yield();
                }

            }
        }).start();
    }

    static void testJoin() {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("join" + i);
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                t1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        t2.start();
    }

    static void testConcurrentHashMap() {
        ConcurrentHashMap<String,String> cmap = new ConcurrentHashMap<>();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 100; i++) {
                cmap.put(""+i,""+i+1);
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 100; i++) {
                cmap.put(""+i+1,""+i);
            }
        });
        t1.start();
        t2.start();
    }

    static void testThread() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(()->{
        });
        Future<?> future = executorService.submit(() -> {
        });
        try {
            Object o = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setAllowCoreThreadTimeOut(true);
        executor.setKeepAliveSeconds(10);
        executor.setQueueCapacity(10);
        executor.setRejectedExecutionHandler(new AbortPolicy());
        executor.initialize();


        ThreadPoolExecutor executor1 = new ThreadPoolExecutor(5,10,
                10,TimeUnit.SECONDS,new LinkedBlockingQueue<>(),new AbortPolicy());


    }

}
